package com.apache.tomcat;

import com.apache.tools.StrUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;

/**
 * description:
 *
 * @author dyh 创建时间：2018年01月29日
 */
@Controller
public class WelcomeAction {

    /**
     * 跳到控制台页面
     */
    @RequestMapping(value = {"/index","/uct/index"}, method = RequestMethod.GET)
    public String console(HttpServletRequest request, HttpServletResponse response,
            Map<String, Object> data) throws ServletException, IOException {
        data.put("title", "基础平台管理控制台");
        data.put("ctx", request.getContextPath());
        data.put("basePath", request.getScheme() + "://" + request.getServerName() + ":" + request
                .getServerPort() + request.getContextPath());
        request.getSession().setAttribute("ctx", request.getContextPath());
        return "/uct/index";
    }

    /**
     * 跳到错误页面
     */
    @RequestMapping(value = {"/error.action","/uct/error.action"})
    public String error(HttpServletRequest request, HttpServletResponse response,
            Map<String, Object> data) {
        data.put("ctx", request.getContextPath());
        data.put("errors", "对不起,您没有访问权限");
        return "/error/error";
    }

    /**
     * 跳到license错误页面
     */
    @RequestMapping(value = {"/errorPage","/uct/errorPage"})
    public String toPage(HttpServletRequest request, Map<String, Object> data) {
        String error = request.getParameter("Errorcode");
        String[] params = new String[0];
        if ("errorCode-00000001".equals(error)) {
            error = getMessage(request, "invalid-001", params, "");
        } else if ("errorCode-00000002".equals(error)) {
            error = getMessage(request, "invalid-002", params, "");
        } else {
            error = getMessage(request, "invalid-003", params, "");
        }
        data.put("ctx", request.getContextPath());
        data.put("Errorcode", error);
        return "/error/invalid";
    }

    /**
     * @return 指定ftl页面
     */
    @RequestMapping("/send/{jspName}")
    public String redirectJsp(HttpServletRequest request, @PathVariable String jspName,
            Map<String, Object> data) {
        setToken(request);
        data.put("ctx", request.getContextPath());
        return "/" + jspName;
    }

    @RequestMapping("/send/{folder}/{jspName}")
    public String redirectJsp(HttpServletRequest request, @PathVariable String folder,
            @PathVariable String jspName, Map<String, Object> data) {
        setToken(request);
        data.put("ctx", request.getContextPath());
        return "/" + folder + "/" + jspName;
    }

    @RequestMapping("/send/{folder1}/{folder2}/{jspName}")
    public String redirectJsp(HttpServletRequest request, @PathVariable String folder1,
            @PathVariable String folder2, @PathVariable String jspName, Map<String, Object> data) {
        setToken(request);
        data.put("ctx", request.getContextPath());
        return "/" + folder1 + "/" + folder2 + "/" + jspName;
    }

    @RequestMapping("/send/{folder1}/{folder2}/{folder3}/{jspName}")
    public String redirectJsp(HttpServletRequest request, @PathVariable String folder1,
            @PathVariable String folder2, @PathVariable String folder3,
            @PathVariable String jspName, Map<String, Object> data) {
        setToken(request);
        data.put("ctx", request.getContextPath());
        return "/" + folder1 + "/" + folder2 + "/" + folder3 + "/" + jspName;
    }

    @RequestMapping("/send/{folder1}/{folder2}/{folder3}/{folder4}/{jspName}")
    public String redirectJsp(HttpServletRequest request, @PathVariable String folder1,
            @PathVariable String folder2, @PathVariable String folder3,
            @PathVariable String folder4, @PathVariable String jspName, Map<String, Object> data) {
        setToken(request);
        data.put("ctx", request.getContextPath());
        return "/" + folder1 + "/" + folder2 + "/" + folder3 + "/" + folder4 + "/" + jspName;
    }

    @RequestMapping("/token")
    @ResponseBody
    public String token(HttpServletRequest request) {
        String uuid = setToken(request);
        return uuid;
    }

    private String setToken(HttpServletRequest request) {
        if (StrUtil.isNotNull(request.getParameter("token"))) {
            HttpSession session = request.getSession(true);
            String uuid = UUID.randomUUID().toString();
            if (null != session)
                session.setAttribute("formToken", uuid);
            return uuid;
        }
        return "";
    }

    /**
     * 获取国际化信息
     *
     * @param msgKey key message_*.properties文件中的key
     * @param params key中{0}、{1}等对应的值
     * @param defaultMessage 对应的默认值
     * @return 返回对应的国际化信息
     */
    private String getMessage(HttpServletRequest request, String msgKey, String[] params,
            String defaultMessage) {
        WebApplicationContext requestContext = RequestContextUtils
                .getWebApplicationContext(request);
        Locale myLocale = RequestContextUtils.getLocale(request);
        String msg = requestContext.getMessage(msgKey, params, defaultMessage, myLocale);
        return StrUtil.doNull(msg, defaultMessage);
    }
}
